import Mock from "mockjs";
var Random = Mock.Random;

Random.extend({
  constellation: function (date) {
    var constellations = ["状态A", "状态B"];
    return this.pick(constellations);
  },
});

const len = 15;

const randomMap = {
  时间: () => Random.date(),
  日期: () => Random.date(),
  编号: () => Random.zip(),
  电话: () => Random.zip(),
  费: () => Random.float(10, 1000),
  数量: () => Random.integer(10, 1000),
  状态: () => Random.constellation(),
  类型: () => Random.last(),
  门: () => Random.cname(),
  人: () => Random.cname(),
  名: () => Random.cname(),
};

export default function () {
  return [
    {
      url: "/mock/list",
      method: "post",
      response: ({ body }) => {
        const { fieldList } = body;
        const resp = {
          id: "@guid",
        };
        for (const field of fieldList) {
          const { dataIndex, title } = field;
          let randomFun = null;
          for (const keyword of Object.keys(randomMap)) {
            if (title.endsWith(keyword)) {
              randomFun = randomMap[keyword];
            }
          }
          // resp[dataIndex] = randomFun?.() ?? Random.csentence(5, 8);
          resp[dataIndex] =
            randomFun ??
            function () {
              return Random.csentence(5, 8);
            };
        }
        return {
          code: 0,
          message: "ok",
          data: {
            [`records|${len}`]: [resp],
            total: 15,
          },
        };
      },
    },
  ];
}
